﻿/*
* Copyright 2009-2010 AKABANA.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
* either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Akabana.Amf.Messaging.Data
{
    /**
     * <summary>リモーティングメッセージ</summary>
     * 
     */
    public class AmfMessage
    {
        /**
         * <summary>リモーティングメッセージを生成する</summary>
         * <param Name="version">AMFのバージョン</param>
         * <returns>リモーティングメッセージ</returns>
         */
        public static AmfMessage CreateInstance(ushort version)
        {
            AmfMessage result = new AmfMessage();
            result.version = version;
            return result;
        }

        /**
         * <summary>AMFバージョン</summary>
         */
        public ushort version { get; private set; }

        /**
         * <summary>ヘッダー数</summary>
         */
        public ushort headerCount { get; private set; }

        /**
         * <summary>ボディー数</summary>
         */
        public ushort bodyCount { get; private set; }

        private ushort _headerIndex;

        private ushort _bodyIndex;

        private AmfMessageHeader[] headers;

        private AmfMessageBody[] bodies;

        private AmfMessage(){
            version = 3;
            _headerIndex = 0;
            _bodyIndex = 0;
        }


        /**
         * <summary>ヘッダーを追加する</summary>
         * <param Name="header">リモーティングメッセージヘッダー</param>
         */
        public void AddHeader(AmfMessageHeader header)
        {
            if (_headerIndex < headerCount)
            {
                headers[_headerIndex++] = header;
            }
        }

        /**
         * <summary>ボディーを追加する</summary>
         * <param Name="body">リモーティングメッセージボディー</param>
         */
        public void AddBody(AmfMessageBody body)
        {
            if (_bodyIndex < bodyCount)
            {
                bodies[_bodyIndex++] = body;
            }
        }

        /**
         * <summary>ボディーを取得する</summary>
         * <param Name="index">ボディーインデックス</param>
         * <returns>リモーティングメッセージボディー</returns>
         */
        public AmfMessageBody GetBodyAt(int index)
        {
            return bodies[index];
        }

        /**
         * <summary>このメッセージのレスポンスを生成する</summary>
         * <returns>リモーティングメッセージ</returns>
         */
        public AmfMessage CreateResponse()
        {
            AmfMessage result = AmfMessage.CreateInstance(version);
            result.Initialize(this.headerCount,this.bodyCount);
            return result;
        }

        public void Initialize(ushort headerCount, ushort bodyCount)
        {
            this.headerCount = headerCount;
            this.bodyCount = bodyCount;
            headers = new AmfMessageHeader[headerCount];
            bodies = new AmfMessageBody[bodyCount];
        }

        public override string ToString()
        {
            return "AmfMessage{version=" + version + ", headerCount=" + headerCount + ", bodyCount=" + bodyCount + ", body=" + bodies + "}";
        }
    }
}
